package com.ruoyi.reportReview.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportReview.domain.bo.IndustrialReportInfoCompanyBo;
import com.ruoyi.reportReview.domain.vo.IndustrialReportInfoCompanyVo;
import com.ruoyi.reportReview.domain.IndustrialReportInfoCompany;
import com.ruoyi.reportReview.mapper.IndustrialReportInfoCompanyMapper;
import com.ruoyi.reportReview.service.IIndustrialReportInfoCompanyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填报对象-物流企业信息Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-23
 */
@RequiredArgsConstructor
@Service
public class IndustrialReportInfoCompanyServiceImpl implements IIndustrialReportInfoCompanyService {

    private final IndustrialReportInfoCompanyMapper baseMapper;

    /**
     * 查询填报对象-物流企业信息
     */
    @Override
    public IndustrialReportInfoCompanyVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询填报对象-物流企业信息列表
     */
    @Override
    public TableDataInfo<IndustrialReportInfoCompanyVo> queryPageList(IndustrialReportInfoCompanyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialReportInfoCompany> lqw = buildQueryWrapper(bo);
        Page<IndustrialReportInfoCompanyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填报对象-物流企业信息列表
     */
    @Override
    public List<IndustrialReportInfoCompanyVo> queryList(IndustrialReportInfoCompanyBo bo) {
        LambdaQueryWrapper<IndustrialReportInfoCompany> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialReportInfoCompany> buildQueryWrapper(IndustrialReportInfoCompanyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialReportInfoCompany> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), IndustrialReportInfoCompany::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getUnifiedSocialCode()), IndustrialReportInfoCompany::getUnifiedSocialCode, bo.getUnifiedSocialCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLegalPerson()), IndustrialReportInfoCompany::getLegalPerson, bo.getLegalPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyType()), IndustrialReportInfoCompany::getCompanyType, bo.getCompanyType());
        lqw.eq(StringUtils.isNotBlank(bo.getMainBusiness()), IndustrialReportInfoCompany::getMainBusiness, bo.getMainBusiness());
        lqw.eq(bo.getRegisterDate() != null, IndustrialReportInfoCompany::getRegisterDate, bo.getRegisterDate());
        lqw.eq(bo.getRegisterAddress() != null, IndustrialReportInfoCompany::getRegisterAddress, bo.getRegisterAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), IndustrialReportInfoCompany::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), IndustrialReportInfoCompany::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), IndustrialReportInfoCompany::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getBelongParkId()), IndustrialReportInfoCompany::getBelongParkId, bo.getBelongParkId());
        lqw.like(StringUtils.isNotBlank(bo.getBelongParkName()), IndustrialReportInfoCompany::getBelongParkName, bo.getBelongParkName());
        lqw.eq(bo.getEmployeeCount() != null, IndustrialReportInfoCompany::getEmployeeCount, bo.getEmployeeCount());
        lqw.eq(StringUtils.isNotBlank(bo.getManager()), IndustrialReportInfoCompany::getManager, bo.getManager());
        lqw.eq(StringUtils.isNotBlank(bo.getManagerContact()), IndustrialReportInfoCompany::getManagerContact, bo.getManagerContact());
        lqw.eq(bo.getLargeScale() != null, IndustrialReportInfoCompany::getLargeScale, bo.getLargeScale());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList1()), IndustrialReportInfoCompany::getFileList1, bo.getFileList1());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList2()), IndustrialReportInfoCompany::getFileList2, bo.getFileList2());
        lqw.eq(bo.getNormalCarCount() != null, IndustrialReportInfoCompany::getNormalCarCount, bo.getNormalCarCount());
        lqw.eq(bo.getNormalCarWeight() != null, IndustrialReportInfoCompany::getNormalCarWeight, bo.getNormalCarWeight());
        lqw.eq(bo.getFreezeCarCount() != null, IndustrialReportInfoCompany::getFreezeCarCount, bo.getFreezeCarCount());
        lqw.eq(bo.getFreezeCarWeight() != null, IndustrialReportInfoCompany::getFreezeCarWeight, bo.getFreezeCarWeight());
        lqw.eq(bo.getDangerousCarCount() != null, IndustrialReportInfoCompany::getDangerousCarCount, bo.getDangerousCarCount());
        lqw.eq(bo.getDangerousCarWeight() != null, IndustrialReportInfoCompany::getDangerousCarWeight, bo.getDangerousCarWeight());
        lqw.eq(bo.getStoreCount() != null, IndustrialReportInfoCompany::getStoreCount, bo.getStoreCount());
        lqw.eq(bo.getStoreArea() != null, IndustrialReportInfoCompany::getStoreArea, bo.getStoreArea());
        lqw.eq(bo.getStoreCapacity() != null, IndustrialReportInfoCompany::getStoreCapacity, bo.getStoreCapacity());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreList()), IndustrialReportInfoCompany::getStoreList, bo.getStoreList());
        lqw.eq(StringUtils.isNotBlank(bo.getDedicatedLineList()), IndustrialReportInfoCompany::getDedicatedLineList, bo.getDedicatedLineList());
        lqw.eq(bo.getOperationWeight() != null, IndustrialReportInfoCompany::getOperationWeight, bo.getOperationWeight());
        lqw.eq(bo.getOperationCars() != null, IndustrialReportInfoCompany::getOperationCars, bo.getOperationCars());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationList()), IndustrialReportInfoCompany::getOperationList, bo.getOperationList());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyLevel()), IndustrialReportInfoCompany::getCompanyLevel, bo.getCompanyLevel());
        return lqw;
    }

    /**
     * 新增填报对象-物流企业信息
     */
    @Override
    public Boolean insertByBo(IndustrialReportInfoCompanyBo bo) {
        IndustrialReportInfoCompany add = BeanUtil.toBean(bo, IndustrialReportInfoCompany.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改填报对象-物流企业信息
     */
    @Override
    public Boolean updateByBo(IndustrialReportInfoCompanyBo bo) {
        IndustrialReportInfoCompany update = BeanUtil.toBean(bo, IndustrialReportInfoCompany.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialReportInfoCompany entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除填报对象-物流企业信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
